//
//  SMGOneViewController.m
//  CaiPiao
//
//  Created by apple on 2018/5/16.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "SMGOneViewController.h"
#import "SMGOneTableViewCell.h"
#import "SMGTwoTableViewCell.h"

@interface SMGOneViewController ()
@property (weak, nonatomic) IBOutlet UITableView *SMGOneListTableView;
@property (nonatomic,retain)NSArray* issueNameArray;
@property (nonatomic,retain)NSMutableArray* listArray;


@property (weak, nonatomic) IBOutlet UIButton *issueNameButton;


@property (weak, nonatomic) IBOutlet UIPickerView *issueNamePickerView;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomCons;

@property (weak, nonatomic) IBOutlet UIButton *PreviousButton;
@property (weak, nonatomic) IBOutlet UIButton *NextButton;


@end

@implementation SMGOneViewController{
    CGFloat bottomConstant;
    NSString* selectedIssueName;
    NSInteger currentIndex;
}

-(void)updateViewConstraints{
    [super updateViewConstraints];
    self.bottomCons.constant = bottomConstant;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    currentIndex = 0;
    self.title = [NSString stringWithFormat:@"%@开奖历史",[LotteryManager sharedManager].currentLotteryName];
    bottomConstant = -1000;
    self.listArray = [NSMutableArray arrayWithCapacity:0];
    
    __weak typeof(self)weakSelf = self;
    if ([[LotteryManager sharedManager].currentLotteryID integerValue]==1) {
        [self.SMGOneListTableView registerNib:[UINib nibWithNibName:@"SMGOneCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"SMGOneTableViewCell"];
        //
    }else{
        [self.SMGOneListTableView registerNib:[UINib nibWithNibName:@"SMGTwoCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"SMGTwoTableViewCell"];
    }

    [[LotteryManager sharedManager] getListWithLotteryID:[LotteryManager sharedManager].currentLotteryID andActionID:@"205" andCallBack:^(BOOL load, NSDictionary *resObj) {
        if (load) {
            weakSelf.issueNameArray = resObj[@"data"];
            NSDictionary* showDic = weakSelf.issueNameArray[0];
            [weakSelf loadSMGListWith:showDic[@"IssueName"]];
        }
    }];
}

-(void)loadSMGListWith:(NSString*)issueName{
    [EasyLodingView showLodingText:@"正在努力加载中..." config:^EasyLodingConfig *{
        return [EasyLodingConfig shared].setLodingType(LodingShowTypeIndicator);
    }];
    
    selectedIssueName = issueName;
    [self.listArray removeAllObjects];
    __weak typeof(self)weakSelf = self;
    
    NSString* actionID = @"";
    if ([[LotteryManager sharedManager].currentLotteryID integerValue]==1) {
        actionID = @"606";
        //
    }else{
        actionID = @"607";
    }
    
    [[LotteryManager sharedManager] getSMGListWithIssueName:issueName andAction:actionID andCallBack:^(BOOL load, NSDictionary *resObj) {
        if (load) {
            [weakSelf.listArray addObjectsFromArray:resObj[@"data"]];
            [weakSelf reloadAction];
        }
    }];
}

-(void)reloadAction{
    [EasyLodingView hidenLoding];
    [self.issueNameButton setTitle:[NSString stringWithFormat:@"%@期(%ld)场",selectedIssueName,[self.listArray count]] forState:UIControlStateNormal];
    if (currentIndex == 0) {
        self.NextButton.userInteractionEnabled = NO;
        [self.NextButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    }else{
        self.NextButton.userInteractionEnabled = YES;
        [self.NextButton setTitleColor:[UIColor colorWithRed:(21/255.0) green:(126/255.0) blue:(251/255.0) alpha:1] forState:UIControlStateNormal];
    }
    if (currentIndex==([self.issueNameArray count]-1)) {
        self.PreviousButton.userInteractionEnabled = NO;
        [self.PreviousButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    }else{
        self.PreviousButton.userInteractionEnabled = YES;
        [self.PreviousButton setTitleColor:[UIColor colorWithRed:(21/255.0) green:(126/255.0) blue:(251/255.0) alpha:1] forState:UIControlStateNormal];
    }
    [self.SMGOneListTableView reloadData];
}


- (IBAction)openIssueNameListAction:(id)sender {
    bottomConstant = 0;
    [self updateViewConstraints];
    [self.issueNamePickerView reloadAllComponents];
}

- (IBAction)doneAction:(id)sender {
    bottomConstant = -1000;
    [self updateViewConstraints];
    [self loadSMGListWith:selectedIssueName];
}

- (IBAction)cancelAction:(id)sender {
    bottomConstant = -1000;
    [self updateViewConstraints];
}

- (IBAction)selecteIssAction:(UIButton *)sender {
    if (sender.tag==0) {
//        self.PreviousButton
        currentIndex++;
    }else{
//        self.NextButton
        currentIndex--;
    }
    NSDictionary* dic = self.issueNameArray[currentIndex];
    [self loadSMGListWith:dic[@"IssueName"]];
}


#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.listArray count];
}

/***
 足球
{
    "WKName": "周二",
    "MNO": "001",
    "LeagueName": "亚冠杯",
    "HTeam": "全北现代",
    "HIcon": "",
    "VTeam": "布里兰",
    "VIcon": "",
    "MatchTime": "2018/05/15 18:00:00",
    "RQ": "-2",
    "RZResult": 3,
    "HRz": "1:0",
    "Rz": "2:0",
    "IssueName": "20180515",
    "IssueNo": "180515001",
    "SPSPF": "1.20,5.30,9.30",
    "SPRQS": "3.10,4.15,1.77,-2",
    "BaseEndTime": "2018-05-15 17:50:00",
    "EndTime": "17:50"
}
 ***/

/***
 篮球
 {
 "WKName": "周二",
 "MNO": "301",
 "LeagueName": "NBA",
 "HTeam": "凯尔特人",
 "HIcon": null,
 "VTeam": "骑士",
 "VIcon": null,
 "MatchTime": "2018/05/16 08:30:00",
 "Rz": "94:107",
 "Rf": "-1.5",
 "RZResult": 1,
 "IssueName": "20180515",
 "IssueNo": "180515301",
 "EndTime": "08:15",
 "DXF": "+205.5"
 }
 ***/

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary* dic = self.listArray[indexPath.row];
    if ([[LotteryManager sharedManager].currentLotteryID integerValue]==1) {
        SMGOneTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"SMGOneTableViewCell" forIndexPath:indexPath];
        cell.dateLabel.text = [NSString stringWithFormat:@"%@ %@ %@ %@",dic[@"WKName"],dic[@"MNO"],dic[@"LeagueName"],dic[@"EndTime"]];
        cell.HTeamLabel.text = dic[@"HTeam"];
        cell.VTeamLabel.text = dic[@"VTeam"];
        cell.RZLabel.text = dic[@"Rz"];
        cell.HRZLabel.text = [NSString stringWithFormat:@"半 %@",dic[@"HRz"]];
        
        NSArray* RZArray = [dic[@"Rz"] componentsSeparatedByString:@":"];
        NSArray* HRZArray = [dic[@"HRz"] componentsSeparatedByString:@":"];
        
        NSInteger RZResult = [RZArray[0] integerValue]-[RZArray[1] integerValue];
        NSInteger totalResult = [RZArray[0] integerValue]+[RZArray[1] integerValue];
        NSInteger HRZResult = [HRZArray[0] integerValue]-[HRZArray[1] integerValue];
        
        NSInteger RQResult = [RZArray[0] integerValue]+[dic[@"Rf"] integerValue]-[RZArray[1] integerValue];
        
        
        //    第一个Label
        NSString* firstString = @"平";
        if (RZResult>0) {
            firstString = @"胜";
        }
        if (RZResult<0){
            firstString = @"负";
        }
        cell.firstLabel.text = firstString;
        
        //    第二个Label
        NSString* secondString = @"平";
        if (RQResult>0) {
            secondString = @"胜";
        }
        if (RQResult<0){
            secondString = @"负";
        }
        cell.secondLabel.text = [NSString stringWithFormat:@"(%@)%@",dic[@"RQ"],secondString];
        //    第三个Label
        cell.thirdLabel.text = dic[@"Rz"];
        //    第四个Label
        cell.fourthLabel.text = [NSString stringWithFormat:@"%ld",totalResult];
        //    第五个Label
        NSString* fifthString = @"平";
        if (HRZResult>0) {
            fifthString = @"胜";
        }
        if (HRZResult<0){
            fifthString = @"负";
        }
        cell.fifthLabel.text = [NSString stringWithFormat:@"%@%@",fifthString,firstString];
        
        return cell;
        //
    }else{
        SMGTwoTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"SMGTwoTableViewCell"];
        cell.dateLabel.text = [NSString stringWithFormat:@"%@ %@ %@ %@",dic[@"WKName"],dic[@"MNO"],dic[@"LeagueName"],dic[@"EndTime"]];
        cell.RZLabel.text = dic[@"Rz"];
        NSArray* RZArray = [dic[@"Rz"] componentsSeparatedByString:@":"];
        NSInteger totalRZResult = [RZArray[0] integerValue]+[RZArray[1] integerValue];
        cell.totalRZLabel.text = [NSString stringWithFormat:@"总分 %ld",totalRZResult];
        cell.HTeamLabel.text = dic[@"HTeam"];
        cell.VTeamLabel.text = dic[@"VTeam"];
        cell.firLabel.text = dic[@"Rz"];
        
        NSInteger RZResult = [RZArray[1] integerValue]-[RZArray[0] integerValue];
        float RQResult = (float)RZResult+[dic[@"Rf"] floatValue];
        
        NSInteger checkBig = totalRZResult-[dic[@"DXF"] integerValue];
        
        NSString* secondString = @"";
        if (RQResult>0) {
            secondString = @"主胜";
        }else{
            secondString = @"客胜";
        }
        cell.secLabel.text = [NSString stringWithFormat:@"(%@)%@",dic[@"Rf"],secondString];
        
        NSString* thirdString = @"";
        if (checkBig>0) {
            thirdString = @"大分";
        }else{
            thirdString = @"小分";
        }
        cell.thirLabel.text = [NSString stringWithFormat:@"(%@)%@",dic[@"DXF"],thirdString];
        
        NSString* forthString = @"";
        if (RZResult>0) {
            forthString = @"主胜";
        }else{
            forthString = @"客胜";
        }
        cell.fothLabel.text = [NSString stringWithFormat:@"%@%@",forthString,[self checkStringWith:ABS(RZResult)]];
        return cell;
    }

}

-(NSString*)checkStringWith:(NSInteger)checkFC{
    NSInteger multiple = checkFC/5;
    
    return [NSString stringWithFormat:@"%ld-%ld",1+multiple*5,5+multiple*5];
}

#pragma mark - UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return [self.issueNameArray count];
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    NSDictionary* dic = self.issueNameArray[row];
    return dic[@"IssueName"];
}

#pragma mark - UIPickerViewDelegate
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    NSDictionary* dic = self.issueNameArray[row];
    currentIndex = row;
    selectedIssueName = dic[@"IssueName"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
